package com.zhugang.week02;

/**
 * @program algorithms
 * @description: middleNode
 * @author: chanzhugang
 * @create: 2022/04/18 23:41
 */
public class MiddleNode {

    public class ListNode {
        public int data;
        public ListNode next;

        public ListNode() {
        }
    }

    /**
     * 链表的中间结点
     * 快慢指针
     */
    public ListNode middleNode(ListNode head) {
        // 快指针每次走两步、慢指针每次走一步
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}